package org.example.laboratorymanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.laboratorymanagement.mapper.NoticeMapper;
import org.example.laboratorymanagement.pojo.Notice;
import org.example.laboratorymanagement.pojo.PageBean;
import org.example.laboratorymanagement.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void delete(List<Integer> ids) {
        noticeMapper.delete(ids);
    }

    @Override
    public void insert(Notice notice) {
        notice.setDate(LocalDateTime.now()); // 获取当前时间
        noticeMapper.insert(notice);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.update(notice);
    }

    @Override
    public PageBean pageNotices(Integer page, Integer pageSize, String title) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);
        // 执行查询
        List<Notice> list = noticeMapper.listNotices(title);
        Page<Notice> p = (Page<Notice>) list;

        // 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }

    @Override
    public List<Notice> selectAll() {
        return noticeMapper.selectAll();
    }
}
